/**
 * Project Name:JavaBaseReview
 * Package Name:com.xuwei.ext.netty.example
 * author:David
 * Date:2016年6月17日上午6:54:50
 * Copyright (c) 2016, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.ext.netty.example;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * Handles a server-side channel.
 * @author David
 * @since 2016年6月17日 上午6:54:50
 * @version 
 * @since JDK 1.6
 */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {

	/*@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		//Discard the recevied data silently.
		 ((ByteBuf) msg).release(); 
	}*/
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		ByteBuf in = (ByteBuf)msg;
		try{
			while(in.isReadable()){
				System.out.println((char)in.readByte());
				System.out.flush();
			}
		}finally{
			ReferenceCountUtil.release(msg);
		}
		
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		//Close the connection when an exception is raised.
		cause.printStackTrace();
		ctx.close();
	}
}
